<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>伪类和伪元素高级应用</title>
    <style>
        :root {
            --primary-color: #007bff;
            --secondary-color: #6c757d;
            --success-color: #28a745;
            --warning-color: #ffc107;
            --danger-color: #dc3545;
            --spacing: 1rem;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            padding: 2rem;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        /* 1. 结构性伪类示例 */
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: var(--spacing);
            margin: 2rem 0;
        }

        .grid-item {
            background: #f8f9fa;
            padding: var(--spacing);
            border-radius: 4px;
        }

        /* 选择第一个和最后一个元素 */
        .grid-item:first-child,
        .grid-item:last-child {
            background: #e9ecef;
        }

        /* 选择奇数项 */
        .grid-item:nth-child(odd) {
            border-left: 3px solid var(--primary-color);
        }

        /* 选择偶数项 */
        .grid-item:nth-child(even) {
            border-left: 3px solid var(--secondary-color);
        }

        /* 2. 状态伪类示例 */
        .custom-input {
            width: 100%;
            padding: 0.5rem;
            margin: 0.5rem 0;
            border: 2px solid #ddd;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .custom-input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
        }

        .custom-input:valid {
            border-color: var(--success-color);
        }

        .custom-input:invalid {
            border-color: var(--danger-color);
        }

        /* 3. 伪元素装饰技巧 */
        .fancy-title {
            position: relative;
            display: inline-block;
            margin: 2rem 0;
        }

        .fancy-title::before,
        .fancy-title::after {
            content: '';
            position: absolute;
            height: 2px;
            background: var(--primary-color);
            top: 50%;
            width: 50px;
        }

        .fancy-title::before {
            right: 100%;
            margin-right: 15px;
        }

        .fancy-title::after {
            left: 100%;
            margin-left: 15px;
        }

        /* 4. 工具提示 */
        .tooltip {
            position: relative;
            display: inline-block;
            cursor: help;
        }

        .tooltip::before {
            content: attr(data-tooltip);
            position: absolute;
            bottom: 100%;
            left: 50%;
            transform: translateX(-50%);
            padding: 0.5rem;
            background: #333;
            color: white;
            border-radius: 4px;
            font-size: 0.875rem;
            white-space: nowrap;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
        }

        .tooltip::after {
            content: '';
            position: absolute;
            bottom: 100%;
            left: 50%;
            transform: translateX(-50%);
            border: 6px solid transparent;
            border-top-color: #333;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
        }

        .tooltip:hover::before,
        .tooltip:hover::after {
            opacity: 1;
            visibility: visible;
            transform: translateX(-50%) translateY(-10px);
        }

        /* 5. 自定义列表样式 */
        .custom-list {
            list-style: none;
            margin: 1rem 0;
        }

        .custom-list li {
            position: relative;
            padding-left: 1.5rem;
            margin: 0.5rem 0;
        }

        .custom-list li::before {
            content: '→';
            position: absolute;
            left: 0;
            color: var(--primary-color);
            transition: transform 0.3s ease;
        }

        .custom-list li:hover::before {
            transform: translateX(5px);
        }

        /* 6. 自定义复选框 */
        .custom-checkbox {
            position: relative;
            padding-left: 35px;
            cursor: pointer;
            display: block;
            margin: 1rem 0;
        }

        .custom-checkbox input {
            position: absolute;
            opacity: 0;
            cursor: pointer;
        }

        .custom-checkbox::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            width: 25px;
            height: 25px;
            border: 2px solid #ddd;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .custom-checkbox::after {
            content: '✓';
            position: absolute;
            left: 7px;
            top: 2px;
            color: white;
            opacity: 0;
            transition: all 0.3s ease;
        }

        .custom-checkbox input:checked ~ ::before {
            background: var(--success-color);
            border-color: var(--success-color);
        }

        .custom-checkbox input:checked ~ ::after {
            opacity: 1;
        }

        /* 7. 自定义光标 */
        .custom-cursor {
            cursor: none;
            position: relative;
            padding: 2rem;
            background: #f8f9fa;
            margin: 1rem 0;
            overflow: hidden;
        }

        .custom-cursor::after {
            content: '👆';
            position: fixed;
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .custom-cursor:hover::after {
            opacity: 1;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="fancy-title">伪类和伪元素高级应用</h1>

        <section>
            <h2>1. 结构性伪类</h2>
            <div class="grid">
                <div class="grid-item">第一个网格项</div>
                <div class="grid-item">第二个网格项</div>
                <div class="grid-item">第三个网格项</div>
                <div class="grid-item">第四个网格项</div>
            </div>
        </section>

        <section>
            <h2>2. 状态伪类</h2>
            <input type="email" class="custom-input" placeholder="请输入邮箱" required>
            <input type="text" class="custom-input" pattern="[A-Za-z]{3,}" placeholder="请输入至少3个字母" required>
        </section>

        <section>
            <h2>3. 工具提示</h2>
            <p>
                这是一个
                <span class="tooltip" data-tooltip="这是一个工具提示示例">工具提示</span>
                示例。
            </p>
        </section>

        <section>
            <h2>4. 自定义列表</h2>
            <ul class="custom-list">
                <li>第一个列表项</li>
                <li>第二个列表项</li>
                <li>第三个列表项</li>
            </ul>
        </section>

        <section>
            <h2>5. 自定义复选框</h2>
            <label class="custom-checkbox">
                选项1
                <input type="checkbox">
            </label>
            <label class="custom-checkbox">
                选项2
                <input type="checkbox">
            </label>
        </section>

        <section>
            <h2>6. 自定义光标</h2>
            <div class="custom-cursor">
                <p>在此区域内移动鼠标查看自定义光标效果</p>
            </div>
        </section>
    </div>
</body>
</html>
